/**
 * 获取location参数
 * @param {*} key 对应的参数key
 * @returns
 */
export const getLocationParam = (key) => {
  if (!window.location.search) return;
  const search = window.location.search.substring(1);
  const params = search.split("&").reduce((obj, str) => {
    const [key, value] = str.split("=");
    return { [key]: value, ...obj };
  }, {});
  return params[key] || null;
};

/**
 *
 * @returns 是否手机环境
 */
export const isPlus = () => {
  return new Promise((resolve) => {
    uni.webView.getEnv((res) => {
      if (res.plus) {
        resolve(true);
      } else {
        resolve(false);
      }
    });
  });
};

// 权限验证
export const permissionVerification = (name, item) => {
  let permission = getLocationParam("permission");
  // 公私海tab栏
  if (name === "SilentLibrary") {
    // 沉寂库 bd  不可见
    if (permission === "bd") {
      return false;
    }
    return true;
  } else if (name === "OuterZone") {
    // 外区商机 bd  lead 不可见
    if (permission === "bd" || permission === "lead") {
      return false;
    }
    return true;
  }
  // if (permission === "lead") return false;
  // 操作按钮
  if (name === "edit") {
    // 跟进 - 新增线上跟进  编辑kp  新增kp
    if (item === "临时库" || item === "私海") return true;
    return false;
  } else if (name === "changeinfo") {
    // 变更信息
    if (permission === "bd") return false;
    if (item === "临时库" || item === "私海" || item === "公海") return true;
    return false;
  } else if (name === "changebd") {
    // 变更bd
    if (permission === "bd") return false;
    if (item === "临时库" || item === "私海") return true;
    return false;
  }
  return false;
};
